.pragma library

function str2date(date) {
    var ret = "";
    if (date.match(/([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/)) {
        ret = date.substring(0, 4) + '/' + date.substring(5, 7) + '/' + date.substring(8, 10) + ' ' + date.substring(11, 16);
    }
    return ret;
}


function str2description(str) {
    var lines = str.split(/\n/);

    var table = false;
    var ul = -1;
    var ol = -1;
    var j;
    var prefix;
    var prev;

    for (var i = 0; i < lines.length; i++) {
        var br = true;
        var line = lines[i];

        if (line == '') {
            if (table) {
                line = '</table>'
                table = false
            }
            if (ul > -1) {
                for (; ul > -1; ul--) {
                    line += '</li></ul>';
                }
            }
            if (ol > -1) {
                for (; ol > -1; ol--) {
                    line += '</li></ol>';
                }
            }
            br = false;
        } else {
            switch (line.charAt(0)) {
            case 'h':
                if (line.charAt(2) == '.' && line.charAt(3) == ' ') {
                    line = '<h' + line.charAt(1) + '>' + line.substring(4) + '</h' + line.charAt(1) + '>';
                }
                br = false;
                break;
            case '|':
                var columns = line.split('|');
                line = '';
                if (!table) {
                    line = '<table style="border=1">';
                    table = true;
                }
                line += '<tr>';
                for (j = 1; j < columns.length - 1; j++) {
                    if (columns[j].substring(0, 3) == '_. ') {
                        line += '<th>' + columns[j].substring(3) + '</th>';
                    } else {
                        line += '<td>' + columns[j] + '</td>';
                    }
                }
                line += '</tr>';
                br = false;
                break;
            case '*':
                prefix = '';
                prev = ul;
                for (j = 0; j < 3; j++) {
                    switch (line.charAt(0)) {
                    case '*':
                        if (ul < j) {
                            prefix += '<ul><li>';
                            ul++;
                        }
                        line = line.substring(1);

                        break;
                    case ' ':
                        if (prev + 1 == j) {
                            prefix += '</li><li>';
                        } else {
                            for (; ul > j; ul--) {
                                prefix += '</li></ul>';
                            }
                        }
                        line = prefix + line.substring(1);
                        j = 4;
                        break;
                    }
                }
                br = false;
                break;
            case '#':
                prefix = '';
                prev = ol;
                for (j = 0; j < 3; j++) {
                    switch (line.charAt(0)) {
                    case '#':
                        if (ol < j) {
                            prefix += '<ol><li>';
                            ol++;
                        }
                        line = line.substring(1);

                        break;
                    case ' ':
                        if (prev + 1 == j) {
                            prefix += '</li><li>';
                        } else {
                            for (; ol > j; ol--) {
                                prefix += '</li></ol>';
                            }
                        }
                        line = prefix + line.substring(1);
                        j = 4;
                        break;
                    }
                }
                br = false;
                break;
            default:
                break;
            }
        }

        line = line.replace(/"([^\"]+)":(https?:\/\/[^\s]+)/g, '<a href="$2">$1</a>');

        if (br)
            line += '<br />';

        lines[i] = line;
    }

    return lines.join("\n")
}
